package com.shanghaionstar.vehicle_ownerlife;

import android.app.Activity;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.shanghaionstar.user.iview.IVehicleInfoView;
import com.shanghaionstar.user.presenter.VehicleInfoPresenter;

/**
 * 驾驶证日期获取和存储统一管理
 *
 * Created by Yonglang Hong on 2017/2/17.
 */
public class VehicleInspectionManager {

    public BaseActivity activity;

    public IVehicleInfoView infoView;

    public VehicleInspectionManager(BaseActivity activity) {
        this.activity = activity;
    }

    public void setView(IVehicleInfoView infoView) {
        this.infoView = infoView;
    }

    /**
     * 车辆信息获取和设置
     */
    private VehicleInfoPresenter vehicleInfoPresenter;

    private VehicleInfoPresenter getVehicleInfoPresenter() {
        if (vehicleInfoPresenter == null) {
            vehicleInfoPresenter = new VehicleInfoPresenter(activity.getScheduler());
            if (infoView != null){
                vehicleInfoPresenter.setView(infoView);
            }else {
                vehicleInfoPresenter.setView(new IVehicleInfoView() {
                    @Override
                    public void setLoading(boolean isLoading) {
                    }
                    @Override
                    public void onResponseError(OnStarException e) {
                    }
                    @Override
                    public void onReponseSuccess(PlateEngineNumEntity object) {
                        if (object != null) {
                            InspectHelper.setLicenseInspectDateAndAlarm(activity,object.getDrivingLicenseDate());
                        }
                    }
                    @Override
                    public Activity onActivity() {
                        return activity;
                    }
                });
            }
        }
        return vehicleInfoPresenter;
    }

    /**
     * 设置驾驶证日期
     */
    public void updateDrivingLicenseDate(String drivingLicenseDate){
        getVehicleInfoPresenter().updateVehicleInfo(drivingLicenseDate,false);
        getVehicleInfoPresenter().start();
    }
    /**
     * 获取驾驶证日期
     */
    public void getDrivingLicenseDate(){
        getVehicleInfoPresenter().updateVehicleInfo("",true);
        getVehicleInfoPresenter().start();
    }

}
